/**
 * Project Name:spring-config-client File Name:GroovyFactory.java Package Name:org.spring.config.client.listener
 * Date:2019年11月1日上午10:30:37 Copyright (c) 2019, dengqian37@126.com All Rights Reserved.
 *
 */

package org.spring.config.client.listener;

/**
 * ClassName:GroovyFactory <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2019年11月1日 上午10:30:37 <br/>
 * 
 * @author dengqian
 * @version
 * @since JDK 1.8
 * @see
 */
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class GroovyFactory implements ApplicationContextAware {

    private String directory;

    private ApplicationContext context;

    public String getDirectory() {
        return directory;
    }

    public void setDirectory(String directory) {
        this.directory = directory;
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {

        if (context == null) {
            this.context = context;
        }

        // // 只有这个对象才能注册bean到spring容器
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getAutowireCapableBeanFactory();
         directory="org/spring/config/client/groovy" ;
//         directory =
//         "F:/workspace/development/config-cloud/spring-config-client/target/classes/org/spring/config/client/groovy";
         
//        directory = "src/main/java/org/spring/config/client/groovy/";

        // 因为spring会自动将xml解析成BeanDefinition对象然后进行实例化，这里我们没有用xml，所以自己定义BeanDefinition
        // 这些信息跟spring配置文件的方式差不多，只不过有些东西lang:groovy标签帮我们完成了
        final String refreshCheckDelay =
            "org.springframework.scripting.support.ScriptFactoryPostProcessor.refreshCheckDelay";

        
        final String proxyTargetClass =
            "org.springframework.scripting.support.ScriptFactoryPostProcessor.proxyTargetClass";
        final String language = "org.springframework.scripting.support.ScriptFactoryPostProcessor.language";
        File root = new File(Thread.currentThread().getContextClassLoader().getResource(".").getFile());

        String realDirectory = Thread.currentThread().getContextClassLoader().getResource(directory).getFile();


        File[] files = new File(realDirectory).listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".groovy") ? true : false;
            }
        });//AbstractRefreshableTargetSource
        for (File file : files) {
            GenericBeanDefinition bd = new GenericBeanDefinition();
            bd.setBeanClassName("org.springframework.scripting.groovy.GroovyScriptFactory");
            // 刷新时间
            bd.setAttribute(refreshCheckDelay, 500);
            // 语言脚本
            bd.setAttribute(language, "groovy");
            bd.setAttribute(proxyTargetClass, true);

            
            

            System.out.println(file.getPath());
            System.out.println(file.getAbsolutePath());

            // 文件目录
            bd.getConstructorArgumentValues().addIndexedArgumentValue(0, file.getPath().replace(root.getPath(), ""));
            // 注册到spring容器org\spring\config\client\groovy\FooImpl.groovy
            // beanFactory.registerBeanDefinition(file.getName().replace(".groovy", ""), bd);
            System.out.println(file.getPath().replace(root.getPath(), ""));
            // bd.getConstructorArgumentValues().addIndexedArgumentValue(0,
            // "F:\\workspace\\development\\config-cloud\\spring-config-client\\target\\classes\\org\\spring\\config\\client\\groovy\\FooImpl.groovy");

            bd.getConstructorArgumentValues().addIndexedArgumentValue(0, "file:src/main/java/org/spring/config/client/groovy/" + file.getName());
            beanFactory.registerBeanDefinition(file.getName().replace(".groovy", ""), bd);
            

        }

        // // 只有这个对象才能注册bean到spring容器
        // DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)
        // context.getAutowireCapableBeanFactory();
        //
        // // 因为spring会自动将xml解析成BeanDefinition对象然后进行实例化，这里我们没有用xml，所以自己定义BeanDefinition
        // // 这些信息跟spring配置文件的方式差不多，只不过有些东西lang:groovy标签帮我们完成了
        // GenericBeanDefinition bd = new GenericBeanDefinition();
        // bd.setBeanClassName("org.springframework.scripting.groovy.GroovyScriptFactory");
        // final String refreshCheckDelay =
        // "org.springframework.scripting.support.ScriptFactoryPostProcessor.refreshCheckDelay";
        // final String language = "org.springframework.scripting.support.ScriptFactoryPostProcessor.language";
        // // 刷新时间
        // bd.setAttribute(refreshCheckDelay, 500);
        // // 语言脚本
        // bd.setAttribute(language, "groovy");
        // // 文件目录 /spring-config-client/src/main/java/FooImpl.groovy
        // bd.getConstructorArgumentValues().addIndexedArgumentValue(0, "file:src/main/java/FooImpl.groovy");
        // // 注册到spring容器
        // beanFactory.registerBeanDefinition("Foo", bd);

    }

   

}
